package com.hanfu.ucenter.web.web.util;


import org.apache.commons.lang.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Base64;
import java.util.Calendar;

public class PayToolUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(PayToolUtil.class);
	
	/**
	 * float 转化 int
	 * @param value
	 * @return
	 */
	public static Integer floatToInt(Float value){
		if(value==null){
			return 0;
		}
		return value.intValue();
	}
	
	/**
	 * 密码加密
	 * @param password
	 * @param salt
	 * @return
	 */
	public static String getEncryptPassword(String password,String salt){
		try {
			return MD5Util.md5(MD5Util.md5(password)+salt);
		} catch (Exception e) {
			logger.error("md5加密错误", e);
		}
		return "";
	}
	
	/**
	 * //浏览器类型：0未知、2微信、3支付宝、4云闪付
	 * @param request
	 * @return
	 */
	public static Integer getBrowserType(HttpServletRequest request){
		String agent=request.getHeader("HTTP_USER_AGENT");
		if(StringUtils.isEmpty(agent)){
			return 4;
		}
		if(agent.contains("MicroMessenger")){
			return 2;
		}else if(agent.contains("Alipay")){
			return 3;
		}else{
			return 4;
		}
	}
	
	/**
	 * 获取到token
	 * @param request
	 * @return
	 */
	public static String getToken(HttpServletRequest request){
		String token=request.getHeader("HTTP_TOKEN");
		if(StringUtils.isEmpty(token)){
			token=request.getParameter("token");
		}
		return token;
	}
	
	/**
	 * //收款码/付款码-加解密方法（type：1加密，2解密）
	 * @param data
	 * @param type
	 * @return
	 */
	public static String analyzQrcode(String data,int type){
		String returnInfo = null;
        //加密
        if(type == 1){
            String codeStr  = jsonEncode(data);
            returnInfo    = base64Encode(codeStr);
        }else if(type == 2){  //解密
        	 String codeStr  = base64Decode(data);
        	 returnInfo    = jsonDecode(codeStr);
        }
        return returnInfo;
	}
	
	/**
	 * json编码
	 * @param json
	 * @return
	 */
	public static String jsonEncode(String json){
		try{
			return StringEscapeUtils.escapeJava(json);
		}catch(Exception ex){
			logger.error("jsonEncode错误",ex);
		}
		return "";
	}
	
	/**
	 * json解码
	 * @param json
	 * @return
	 */
	public static String jsonDecode(String json){
		try{
			return StringEscapeUtils.unescapeJava(json);
		}catch(Exception ex){
			logger.error("jsonDecode错误",ex);
		}
		return "";
	}
	
	/**
	 * base64 编码
	 * @param str
	 * @return
	 */
	public static String base64Encode(String str){
		try{
			return new String(Base64.getEncoder().encode(str.getBytes()));
		}catch(Exception ex){
			logger.error("base64Encode错误",ex);
		}
		return "";
    }
	
	/**
	 * base64 编码
	 * @param str
	 * @return
	 */
	public static String base64Decode(String str){
		try{
			return new String(Base64.getDecoder().decode(str));
		}catch(Exception ex){
			logger.error("base64Decode错误",ex);
		}
		return "";
    }
	
	public static Integer getNowTime(){
		Long time = Calendar.getInstance().getTimeInMillis()/1000;
		return time.intValue();
	}
	
	

}
